<template>
  <div class="recommend-music">
    <van-swipe
      class="recommend-music-swiper"
      :autoplay="3000"
      indicator-color="#61CED7"
    >
      <van-swipe-item v-for="(item, index) in advBanner" :key="index">
        <img :src="item" alt="" />
      </van-swipe-item>
    </van-swipe>
    <div
      class="recommend-music-list"
      v-for="(sub_item, sub_index) in subMusicList"
      :key="sub_index"
    >
      <div class="recommend-music-list-title">
        <div class="recommend-music-list-title-tag">{{ sub_item.title }}</div>
        <div class="recommend-music-list-title-icon"></div>
      </div>
      <div class="recommend-music-list-body">
        <div
          v-for="(song_item, song_index) in sub_item.musics"
          :key="song_index"
          class="recommend-music-list-body-item"
          @click="toMusicListDetail(song_item.id)"
        >
          <div class="recommend-music-list-body-item-pic">
            <img :src="song_item.coverImgUrl" alt="" />
            <div class="recommend-music-list-body-item-pic-intro">
              {{ formatPlayTimes(song_item.playCount) }}次播放
            </div>
          </div>
          <div class="recommend-music-list-body-item-name">
            {{ song_item.name }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, computed } from "vue";
import { httpService } from "@/core/http";
import api from "@/api";
import { useCurrentInstance } from "@/utils/hooks";

/** 歌单基础类型 */
export interface MusicListProps {
  id: string;
  name: string;
  coverImgUrl: string;
}

/** 歌单分类类型 */
export interface SubMusicListProps {
  title: string;
  code: string;
  musics: Array<MusicListProps | undefined>;
}

interface StateProps {
  advBanner: string[];
  subMusicList: SubMusicListProps[];
}

export default defineComponent({
  name: "RecommendMusic",
  setup() {
    const _this = useCurrentInstance();
    const state = reactive<StateProps>({
      advBanner: [
        require("../images/banner1.png"),
        require("../images/banner2.png"),
      ],
      subMusicList: [
        { title: "推荐歌单", code: "推荐", musics: [] },
        { title: "日韩歌单", code: "日韩", musics: [] },
        { title: "欧美歌单", code: "欧美", musics: [] },
        { title: "粤语歌单", code: "粤语", musics: [] },
      ],
    });
    /** 获取歌单 */
    const getMusicList = async () => {
      const { subMusicList } = state;
      subMusicList.forEach(async (sub) => {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const res: any = await getMusicListByWords(sub.code);
        if (res.result && res.result.playlists) {
          sub.musics = res.result.playlists;
        }
      });
      state.subMusicList = subMusicList;
    };
    /** 获取推荐歌单 3首 */
    /** 获取日韩歌单 3首 */
    /** 获取欧美歌单 3首 */
    /** 获取粤语歌单 3首 */
    const getMusicListByWords = async (words: string) => {
      return httpService("GET", api.search, {
        csrf_token: "hlpretag=",
        hlposttag: "",
        type: 1000,
        offset: 0,
        total: true,
        limit: 3,
        s: words,
      });
    };
    /** 格式化播放次数 */
    const formatPlayTimes = computed(() => (times: number) => {
      return times > 10000 ? `${Math.floor(times / 10000)}万` : times;
    });
    /** 跳转歌单详情页面 */
    const toMusicListDetail = (playlistId: string) => {
      _this.goPage('musiclist',{id:playlistId})
    };
    getMusicList();
    return {
      ...toRefs(state),
      getMusicListByWords,
      getMusicList,
      formatPlayTimes,
      toMusicListDetail
    };
  },
});
</script>

<style lang='scss' scoped>
.recommend-music {
  height: 100%;
  overflow: auto;
  .recommend-music-swiper {
    height: 150px;
    :deep() .van-swipe-item {
      background: #f2f2f2;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .recommend-music-list {
    .recommend-music-list-title {
      height: 35px;
      padding: 0 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-top: 1px solid #f2f2f2;
      border-bottom: 1px solid #f2f2f2;
      .recommend-music-list-title-tag {
        display: flex;
        align-items: center;
        color: #707070;
        font-size: 14px;
        font-weight: 600;
        line-height: 20px;
        &::before {
          @include systemBackColor();
          content: "";
          width: 3px;
          height: 18px;
          border-radius: 5px;
          margin-right: 5px;
        }
      }
      .recommend-music-list-title-icon {
        width: 15px;
        height: 15px;
        background-size: 100% 100%;
        background-repeat: no-repeat;
        background-image: url("../images/jiantou.svg");
      }
    }
    .recommend-music-list-body {
      padding: 10px;
      min-height: 150px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .recommend-music-list-body-item {
        width: 110px;
        .recommend-music-list-body-item-pic {
          height: 110px;
          border-radius: 5px;
          overflow: hidden;
          position: relative;
          img {
            width: 100%;
            height: 100%;
          }
          .recommend-music-list-body-item-pic-intro {
            display: flex;
            align-items: center;
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            height: 18px;
            color: #fff;
            font-size: 10px;
            background-color: rgba(0, 0, 0, 0.5);
            &::before {
              content: "";
              width: 12px;
              height: 12px;
              background-image: url("../images/music.svg");
              background-size: 100% 100%;
              background-repeat: no-repeat;
              margin: 0 2px 0 5px;
            }
          }
        }
        .recommend-music-list-body-item-name {
          font-weight: 600;
          height: 30px;
          margin-top: 5px;
          text-align: justify;
          text-align-last: left;
          color: #707070;
          font-size: 12px;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
      }
    }
  }
}
</style>